package kuaidi.view.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import kuaidi.entity.kuaidijieguo.kefu.KeFu;
import kuaidi.helper.R;

/**
 * Created by Jhon on 2016/11/3.
 */

public class KeFuAdapter extends RecyclerView.Adapter<KeFuAdapter.KeFuViewHolder> implements View.OnClickListener {
    private List<KeFu> data;
    private Context context;
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    public KeFuAdapter(List<KeFu> data, Context context) {
        this.data = data;
        this.context = context;
    }

    @Override
    public KeFuViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_kefu_layout, null);
        KeFuViewHolder keFuViewHolder = new KeFuViewHolder(view);
        view.setOnClickListener(this);
        return keFuViewHolder;
    }

    @Override
    public void onBindViewHolder(KeFuViewHolder holder, int position) {
        holder.itemView.setTag(data.get(position));
        Glide.with(context).load(data.get(position).getUrl()).placeholder(R.mipmap.ic_launcher).into(holder.kefuCompanyIv);
        holder.kefuCompanyTv.setText(data.get(position).getName());
        holder.kefuPhoneTv.setText(data.get(position).getPhone());
    }

    @Override
    public int getItemCount() {
        return data != null ? data.size() : 0;
    }

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view, (KeFu) view.getTag());
        }
    }

    public class KeFuViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.kefu_company_iv)
        ImageView kefuCompanyIv;
        @BindView(R.id.kefu_company_tv)
        TextView kefuCompanyTv;
        @BindView(R.id.kefu_phone_tv)
        TextView kefuPhoneTv;

        public KeFuViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnRecyclerViewItemClickListener {
        void onItemClick(View view, KeFu data);
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

}
